package com.example.demo.controller;

import com.example.demo.model.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@RestController
public class MyRedisController {



    private final String User_Redis_key1 = "UserKey1";

    private final String User_Redis_key2 = "UserKey2";

    @Resource
    private ObjectMapper objectMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/setstr")
    public String setStr(String key, String value) {
        if (StringUtils.hasLength(key) && StringUtils.hasLength(value)) {
            //将字符串存储到redis
            stringRedisTemplate.opsForValue().set(key, value);
            return "操作 redis 成功 ！";
        } else {
            return "操作 redis 失败 ！";
        }
    }

    @RequestMapping("/getstr")
    public String grtStr(String key) {
        if (StringUtils.hasLength(key)) {
            String ret = "";
            ret = stringRedisTemplate.opsForValue().get(key);
            return "获取的value为：" + ret;
        } else {
            return "参数有误，操作 redis 失败";
        }
    }


    @RequestMapping("/setuser")
    public String setUser() {
        User user = new User();
        user.setId(1);
        user.setName("tanchen");
        user.setAge(18);

        try {
            String userstr = objectMapper.writeValueAsString(user);
            stringRedisTemplate.opsForValue().set(User_Redis_key1, userstr);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "操作 redis 成功 ！";
    }

    @RequestMapping("/getuser")
    public String getUser(){
        String userstr = stringRedisTemplate.opsForValue().get(User_Redis_key1);

        try {
            User user = objectMapper.readValue(userstr,User.class);
            return "UserMessage:" + user;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "操作 redis 失败 ！";
    }



    @RequestMapping("/sethash")
    public String setHash(){
        Map<String,String> map = new HashMap<>();
        map.put("ID","209000804");
        map.put("name","tanchen");
        map.put("age","18");
        stringRedisTemplate.opsForHash().putAll(User_Redis_key2,map);
        return "do redis success !";
    }

    @RequestMapping("/gethash")
    public String getHash(){
        //返回值:存储了键值对的一个map
        Map<Object, Object> resultMap = stringRedisTemplate.opsForHash().entries(User_Redis_key2);
        return "do redis failed !" + resultMap;
    }

}
